//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.ky;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.CardTypeCodeDao;
import com.aidong.dao.wills.DicShopAreaCodeDao;
import com.aidong.dao.wills.KyCardDao;
import com.aidong.dao.wills.KyComboCodeDao;
import com.aidong.dao.wills.KyContractDao;
import com.aidong.model.wills.CardTypeCode;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.KyCard;
import com.aidong.model.wills.KyComboCode;
import com.aidong.model.wills.KyContract;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PayList;
import com.aidong.util.ExportXls;
import com.aidong.web.controller.member.PayController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"kyContract"})
public class KyContractController extends PayController {
    @Resource
    KyContractDao kyContractDao;
    @Resource
    KyComboCodeDao kyComboCodeDao;
    @Resource
    KyCardDao kyCardDao;
    @Resource
    CardTypeCodeDao cardTypeCodeDao;
    @Resource
    DicShopAreaCodeDao dicShopAreaCodeDao;

    public KyContractController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return (new ModelAndView("ky/contract/list")).addObject("shopName", this.getTypeList("shopName")).addObject("saleType", this.getTypeList("ptSaleType")).addObject("dt1", sdf.format(new Date(System.currentTimeMillis() - 259200000L))).addObject("dt2", sdf.format(new Date()));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleType",required = false) String saleType, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "salesCode",required = false) String salesCode, @RequestParam(value = "dateType",required = false) String dateType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.eq("c.saleType", saleType);
        cd.eq("c.memberCode", memberCode);
        cd.eq("c.cardCode", cardCode);
        ConditionMap mv;
        if(!this.chkNull(salesCode)) {
            mv = new ConditionMap(1);
            mv.eq("c.salesCode", salesCode);
            mv.eq("c.salesCode2", salesCode);
            cd.addSub(mv);
        }

        if(this.queryOwn()) {
            mv = new ConditionMap(1);
            mv.eq("c.salesCode", this.getLoginUserCode());
            mv.eq("c.salesCode2", this.getLoginUserCode());
            mv.eq("c.createUser", this.getLoginUserCode());
            cd.addSub(mv);
        }

        if(!this.chkNull(dateFrom)) {
            cd.ge("c.saleDate", this.getDateFrom(dateFrom));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("c.saleDate", this.getDateTo(dateTo));
        }

        this.kyContractDao.queryPageList(pg, cd);
        ModelAndView mv1 = new ModelAndView("ky/contract/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleType",required = false) String saleType, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "salesCode",required = false) String salesCode, @RequestParam(value = "dateType",required = false) String dateType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, '\uea60');
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            this.addConditionShopCode(cd, "c.shopCode", shopCode);
            cd.eq("c.saleType", saleType);
            cd.eq("c.memberCode", memberCode);
            cd.eq("m.cardCode", cardCode);
            ConditionMap xls;
            if(!this.chkNull(salesCode)) {
                xls = new ConditionMap(1);
                xls.eq("c.salesCode", salesCode);
                xls.eq("c.salesCode2", salesCode);
                cd.addSub(xls);
            }

            if(this.queryOwn()) {
                xls = new ConditionMap(1);
                xls.eq("c.salesCode", this.getLoginUserCode());
                xls.eq("c.salesCode2", this.getLoginUserCode());
                xls.eq("c.createUser", this.getLoginUserCode());
                cd.addSub(xls);
            }

            if(!this.chkNull(dateFrom)) {
                cd.ge("c.saleDate", this.getDateFrom(dateFrom));
            }

            if(!this.chkNull(dateTo)) {
                cd.le("c.saleDate", this.getDateTo(dateTo));
            }

            this.kyContractDao.queryPageList(e, cd);
            ExportXls xls1 = new ExportXls(response, "K-Yoga销售流水", "K-Yoga销售流水");
            xls1.addHead(new String[]{"门店编号", "门店名称", "销售编号", "销售日期", "K-Yoga卡号", "会员号", "会员姓名", "购买金额", "赠送金额", "有效日期", "过期使用", "收款金额", "定金转入", "销售员", "销售员2", "操作员", "备注"});
            xls1.setWidth(new int[]{10, 10, 10, 20, 12, 18, 10, 10, 10, 12, 10, 10, 10, 15, 15, 15, 20});
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Iterator i$ = e.getResultList().iterator();

            while(i$.hasNext()) {
                ContractInfo contract = (ContractInfo)i$.next();
                xls1.addRow(new Object[]{contract.getShopCode(), this.cache.getValue("shopName", contract.getShopCode()), contract.getSaleId(), sdf2.format(contract.getSaleDate()), contract.getCardCode(), contract.getMemberCode(), contract.getMemberName(), contract.getFactAmount(), contract.getPresentAmount(), sdf.format(contract.getEndTime()), this.chkNullZ(contract.getOverDueUse())?"":"是", contract.getPayAmount(), contract.getEarnestAmount(), this.nvl(contract.getSalesCode()) + " - " + this.nvl(this.cache.getValue("salesName", contract.getSalesCode())), this.nvl(contract.getSalesCode2()) + " - " + this.nvl(this.cache.getValue("salesName", contract.getSalesCode2())), this.nvl(contract.getCreateUser()) + " - " + this.nvl(this.cache.getValue("userName", contract.getCreateUser())), contract.getNote()});
            }

            xls1.close();
            this.addUserLog("导出K-Yoga销售流水", e.getResultList().size() + "条记录");
        } catch (Exception var17) {
            this.log.error("import", var17);
        }

        return null;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        List comboList = this.kyComboCodeDao.getShopComboList(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("ky/contract/add");
        mv.addObject("comboList", comboList);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("contract") KyContract contract, @RequestParam("payls") String payls) {
        if(this.chkNull(contract.getMemberCode())) {
            return this.getResult(false, "请指定会员");
        } else if(this.chkNull(contract.getComboCode())) {
            return this.getResult(false, "请选择套餐");
        } else if(this.chkNull(contract.getCardCode())) {
            return this.getResult(false, "请输入K-Yoga卡号");
        } else {
            KyComboCode combo = (KyComboCode)this.kyComboCodeDao.getById(contract.getComboCode());
            if(combo == null) {
                return this.getResult(false, "选择的套餐错误");
            } else {
                contract.setFactAmount(combo.getChargeAmount());
                contract.setPresentAmount(combo.getPresentAmount());
                contract.setOverDueUse(combo.getOverDueUse());
                MemberInfo member = this.memberInfoDao.getByMemberCode(contract.getMemberCode());
                if(member == null) {
                    return this.getResult(false, "会员信息错误");
                } else {
                    if(this.chkNull(member.getKyogaCard()) || !member.getKyogaCard().equalsIgnoreCase(contract.getCardCode())) {
                        KyCard payList = this.kyCardDao.getById(contract.getCardCode());
                        if(payList == null || !payList.getShopCode().equalsIgnoreCase(this.getLoginShopCode()) || payList.getStatus().intValue() != 1) {
                            return this.getResult(false, "K-Yoga卡号错误");
                        }
                    }

                    if(!this.chkNullZ(contract.getEarnestId())) {
                        MemberEarnest payList1 = (MemberEarnest)this.memberEarnestDao.getById(contract.getEarnestId());
                        if(payList1 == null || payList1.getStatus().intValue() != 0 || !payList1.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                            return this.getResult(false, "定金不可用");
                        }

                        contract.setEarnestAmount(payList1.getEarnestAmount());
                    } else {
                        contract.setEarnestId((Integer)null);
                        contract.setEarnestAmount(Double.valueOf(0.0D));
                    }

                    contract.setShopCode(this.getLoginShopCode());
                    contract.setSaleType("1");
                    contract.setSaleDate(new Date());
                    contract.setUseAmount(Double.valueOf(0.0D));
                    contract.setInAmount(Double.valueOf(0.0D));
                    contract.setPayAmount(Double.valueOf(contract.getFactAmount().doubleValue() - contract.getEarnestAmount().doubleValue()));
                    contract.setStatus(Integer.valueOf(1));
                    contract.setSignStatus(Integer.valueOf(this.chkNullZ(contract.getSignStatus())?2:0));
                    contract.setPrintLabel(Integer.valueOf(0));
                    contract.setCreateUser(this.getLoginUserCode());
                    contract.setCreateTime(new Date());
                    contract.setUpdateTime(new Date());
                    ArrayList payList2 = new ArrayList();
                    StringBuilder payMsg = new StringBuilder();
                    PayList pay = this.getPayList(payls, payList2, "12", payMsg);
                    if(pay == null) {
                        return this.getResult(false, payMsg.toString());
                    } else if(!this.equals(pay.getAmount().doubleValue(), contract.getPayAmount().doubleValue())) {
                        return this.getResult(false, "付款金额不一致");
                    } else {
                        contract.setSalesCode(pay.getSalesCode());
                        contract.setSalesCode2(pay.getSalesCode2());

                        try {
                            this.kyContractDao.addContract(contract, member, payList2);
                            this.addUserLog("新增K-Yoga合同", "合同号:" + contract.getSaleId() + ",会员:" + contract.getMemberCode());
                        } catch (Exception var9) {
                            this.log.error("add kycontract", var9);
                            return this.getResult(false, "保存发生错误");
                        }

                        return this.getSuccess(String.valueOf(contract.getSaleId()), "保存完成");
                    }
                }
            }
        }
    }

    @RequestMapping({"getMemberKy"})
    @ResponseBody
    public String getMemberKy(@RequestParam("cardCode") String cardCode) {
        MemberInfo member = this.memberInfoDao.getByCardCode(cardCode);
        if(member == null) {
            return this.getResult(false, "会员卡号错误");
        } else if(!this.chkc(member.getMemberCode())) {
            return this.getResult(false, "会员无本店会籍");
        } else {
            MemberContract contract = this.memberContractDao.getByMemberCode(member.getMemberCode());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String contractEnd = "";
            if(contract != null) {
                if(contract.getNewEndTime() != null) {
                    contractEnd = sdf.format(contract.getNewEndTime());
                } else if(contract.getEndTime() != null) {
                    contractEnd = sdf.format(contract.getEndTime());
                }
            }

            KyContract kyContract = this.kyContractDao.getValidAmountByMemberCode(member.getMemberCode(), this.getLoginShopCode());
            StringBuilder sb = new StringBuilder();
            sb.append("{code:\"").append(member.getMemberCode()).append("\"").append(",card:\"").append(member.getCardCode()).append("\"").append(",shop:\"").append(member.getShopCode()).append("\"").append(",name:\"").append(member.getChName()).append("\"").append(",gender:\"").append(member.getGender()).append("\"").append(",genderName:\"").append(this.cache.getValue("gender", member.getGender())).append("\"").append(",idType:\"").append(member.getIdType()).append("\"").append(",idTypeName:\"").append(this.cache.getValue("idType", member.getIdType())).append("\"").append(",idNumber:\"").append(member.getIdNumber()).append("\"").append(",mobile:\"").append(member.getMobile()).append("\"").append(",contractEnd:\"").append(contractEnd).append("\"").append(",kyogaCard:\"").append(this.nvl(member.getKyogaCard())).append("\"").append(",kyFactAmount:\"").append(kyContract == null?"":this.nvl(kyContract.getFactAmount())).append("\"").append(",kyPresentAmount:\"").append(kyContract == null?"":this.nvl(kyContract.getPresentAmount())).append("\"").append(",kyContractEnd:\"").append(kyContract != null && kyContract.getEndTime() != null?sdf.format(kyContract.getEndTime()):"").append("\"").append(",memberPhoto:\"").append(this.nvl(member.getMemberPhoto())).append("\"").append(",idCardPhoto:\"").append(this.nvl(member.getIdCardPhoto())).append("\"").append("}");
            return this.getSuccess(sb.toString(), "");
        }
    }

    @RequestMapping({"getKyCardList"})
    public ModelAndView getCardList() {
        List list = this.kyCardDao.getNotUseList(this.getLoginShopCode());
        List lstp = this.kyCardDao.getNotUseTypeCount(this.getLoginShopCode());
        return (new ModelAndView("member/card")).addObject("list", list).addObject("lstp", lstp);
    }

    @RequestMapping({"getKyCardNew"})
    @ResponseBody
    public String getCardNew(@RequestParam("cardCode") String cardCode) {
        KyCard card = this.kyCardDao.getByCardCode(cardCode);
        if(card != null && card.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
            if(card.getStatus().intValue() != 1) {
                return this.getResult(false, "卡号已使用,请选择其他卡号");
            } else {
                StringBuilder sb = new StringBuilder();
                sb.append("{cardCode:\"").append(card.getCardCode()).append("\"").append(",memberCode:\"").append(this.nvl(card.getMemberCode())).append("\"").append(",status:").append(card.getStatus()).append("").append("}");
                return this.getSuccess(sb.toString(), "");
            }
        } else {
            return this.getResult(false, "没有这个卡号");
        }
    }

    private boolean chkc(String memberCode) {
        List list = this.memberContractDao.getCurrentListByMemberCode(memberCode);
        if(list != null && list.size() != 0) {
            String shopCode = this.getLoginShopCode();
            Iterator i$ = list.iterator();

            while(i$.hasNext()) {
                MemberContract contract = (MemberContract)i$.next();
                if(shopCode.equalsIgnoreCase(contract.getShopCode())) {
                    return true;
                }

                if(this.memberContractDao.chkContractShop(contract.getContractCode(), shopCode)) {
                    return true;
                }

                if(!this.chkNull(contract.getManyShop())) {
                    CardTypeCode cardType = (CardTypeCode)this.cardTypeCodeDao.getById(contract.getCardType());
                    if(cardType != null && cardType.getManyShopQuantity().intValue() == 0 && this.dicShopAreaCodeDao.chkAreaShop(contract.getManyShop(), shopCode)) {
                        return true;
                    }
                }
            }

            return false;
        } else {
            return false;
        }
    }
}
